/**
 * 
 */
package com.pmsco.pms.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsco.pms.ajaxdata.MedicineJsonData;
import com.pmsco.pms.ajaxdata.SearchDrugResultPojo;
import com.pmsco.pms.business.MedicineBusiness;
import com.pmsco.pms.business.dto.MedicineRequestDTO;
import com.pmsco.pms.business.dto.MedicineReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.Medicine;
import com.pmsco.pms.core.domain.MedicineCategory;

/**
 * @author Tran Thanh Long
 *
 */
@Controller
public class PmsRestController extends AbstractPmsController {
	private static final Logger logger = LoggerFactory.getLogger(PmsRestController.class);
	private MedicineBusiness medicineBusiness = new MedicineBusiness();
	
	/**
	 * Search drug information
	 */
	@RequestMapping(value = PmsConstant.searchDrug_RestUrl, method = RequestMethod.GET)
	public @ResponseBody SearchDrugResultPojo searchDrug(@RequestParam String searchText, @RequestParam Integer page, @RequestParam Integer rows) {
		logger.info("Search drug information");
		SearchDrugResultPojo ret = new SearchDrugResultPojo();
		
		MedicineRequestDTO medicineRequestDTO = new MedicineRequestDTO();
		String sidx = "name";
		String sord = "desc";
		String searchField = "name";
		String searchOper = "cn";
		if(page == null)
			page = 1;
		if(rows == null)
			rows = 10;
		medicineRequestDTO.setPage(page);
		medicineRequestDTO.setRows(rows);
		medicineRequestDTO.setSidx(sidx);
		medicineRequestDTO.setSord(sord);
		medicineRequestDTO.setSearchField(searchField);
		medicineRequestDTO.setSearchString(searchText);
		medicineRequestDTO.setSearchOper(searchOper);
		
		logger.debug("Call to pms medicine business for list medicine");
		MedicineReturnDTO medicineReturnDTO = medicineBusiness.listMedicineData(medicineRequestDTO);
		
		logger.debug("Analysis list medicine return dto");
		Integer records = medicineReturnDTO.getMedicinesSize();
		Integer total = (records / rows);
		if(records % rows > 0)
			total += 1;
		ret.setPage(page);
		ret.setRecords(records);
		ret.setTotal(total);
		List<MedicineJsonData> data = new ArrayList<MedicineJsonData>();
		MedicineJsonData jsonData = null;
		for(Medicine medicine : medicineReturnDTO.getMedicines()) {
			jsonData = new MedicineJsonData(medicine);
			jsonData.setDispenseType(medicine.getDispenseType());
			jsonData.setManufacturer(medicine.getManufacturer());
			jsonData.setDistributor(medicine.getDistributor());
			jsonData.setImporter(medicine.getImporter());
			if(medicine.getMainMedicineCategories() != null && medicine.getMainMedicineCategories().size() > 0) {
				MedicineCategory mainMedicineCategory = medicine.getMainMedicineCategories().get(0);
				jsonData.setCategory(mainMedicineCategory.getCategory().getName());
				jsonData.setSubCategory(mainMedicineCategory.getSubCategory().getName());
			}
			data.add(jsonData);
		}
		ret.setSearchResults(data);
		return ret;
	}
}
